package com.cy.store.controller;

import com.cy.store.entity.Address;
import com.cy.store.entity.Areas;
import com.cy.store.entity.Cities;
import com.cy.store.entity.Provinces;
import com.cy.store.service.IAddressService;
import com.cy.store.service.impl.AreasService;
import com.cy.store.service.impl.CityService;
import com.cy.store.service.impl.ProvincesService;
import com.cy.store.util.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @author ZhangSir
 */
@RestController
@RequestMapping("/addresses")
public class AddressController extends BaseController{
    @Autowired
    private IAddressService addressService;
    @Autowired
    private ProvincesService provincesService;
    @Autowired
    private CityService cityService;
    @Autowired
    private AreasService areasService;
    @ApiOperation("/* 添加收货地址 */")
    @RequestMapping(value = "/add_new_address",method = POST)
    public JsonResult<String> addAddress(HttpSession session, Address address){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.addNewAddress(uid,username,address);
        return new JsonResult<>(OK);
    }

    @ApiOperation("设置默认收货地址")
    @RequestMapping(value = "/set_default_address/{aid}",method = POST)
    public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid,
                                       HttpSession session){
         addressService.setDefault(aid,
                                    getUidFromSession(session),
                                    getUsernameFromSession(session));
         return new JsonResult<>(OK);
    }

    @ApiOperation("获取所有省份")
    @RequestMapping(value = "/getProvinces",method = GET)
    public JsonResult<List> getProvinces(){
        List<Provinces> provinces = provincesService.getProvinces();
        return new JsonResult<>(OK,provinces);
    }

    @ApiOperation("根据省份编码获取所有城市")
    @RequestMapping(value = "/getCities/{provinceCode}",method = GET)
    public JsonResult<List> getCities(@PathVariable("provinceCode") String provinceCode){
        List<Cities> cities = cityService.getCityByProvinceCode(provinceCode);
        return new JsonResult<>(OK,cities);
    }

    @ApiOperation("根据城市编码获取所有区域")
    @RequestMapping(value = "/getAreas/{cityCode}",method = GET)
    public JsonResult<List> getAreas(@PathVariable("cityCode") String cityCode){
        List<Areas> areas = areasService.getAreasByCityCode(cityCode);
        return new JsonResult<>(OK,areas);
    }
}
